package echat.future.cn.echat.my.view;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;

import com.google.gson.Gson;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import echat.future.cn.echat.R;
import echat.future.cn.echat.databinding.ActivityEditeAuthUserBinding;
import echat.future.cn.echat.my.api.PutObjectSamplesApi;
import echat.future.cn.echat.my.api.UserApi;
import echat.future.cn.echat.my.bean.BaseVo;
import echat.future.cn.echat.my.bean.ImageBean;
import echat.future.cn.echat.my.utils.HttpUtilsRequst;
import echat.future.cn.echat.my.utils.UserHelper;
import echat.future.cn.echat.my.weight.Navagation.AdapterNavagation;
import echat.future.cn.echat.my.weight.Navagation.IBaseNavagation;
import echat.future.cn.echat.my.weight.ProgrossDialog;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class EditeAuthUserActivity extends BaseActivity<ActivityEditeAuthUserBinding> implements TextWatcher {

    @Override
    public int layoutId() {
        return R.layout.activity_edite_auth_user;
    }

    @Override
    public IBaseNavagation navagation() {
        return new AdapterNavagation(this).setTitle("编辑认证资料", 16, R.color.white).setBack(R.drawable.btn_back).setNavagationBackgroudColor(R.color.color_161823);
    }

    @Override
    public void initView() {
        bind.confirm.setOnClickListener(v -> {
            authUser2();
        });
        bind.wx.addTextChangedListener(this);
        bind.phone.addTextChangedListener(this);
        bind.picSelectorView.setOnPicCountChangeListenre(() -> {
            overInput();
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        bind.picSelectorView.onActivityResult(requestCode, resultCode, data);
        overInput();
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        overInput();
    }

    private void overInput() {
        bind.confirm.setEnabled(false);
        if (bind.picSelectorView.getSelectorUrls().size() == 1) {
            return;
        }
        if (bind.phone.getText().toString().equals("")) {
            return;
        }
        if (bind.wx.getText().toString().equals("")) {
            return;
        }
        bind.confirm.setEnabled(true);
    }


    /**
     * 提交认证信息
     */
    private void authUser() {
//        List files = new ArrayList();
//        for (int i = 0;i<bind.picSelectorView.getSelectorUrls().size();i++) {
//            if(bind.picSelectorView.getSelectorUrls().get(i).getItemType() == 0){
//                continue;
//            }
//            MultipartBody.Part filr = null;
//            try {
//                RequestBody requestBody = RequestBody.create(MediaType.parse("image/jpeg"), new File(bind.picSelectorView.getSelectorUrls().get(i).getImg()));
//                filr = MultipartBody.Part.createFormData("auth_img["+i+"]", new File(bind.picSelectorView.getSelectorUrls().get(i).getImg()).getName(), requestBody);
//            } catch (Exception e) {
//                continue;
//            }
//            files.add(filr);
//        }
//        MultipartBody.Part[] strings = new MultipartBody.Part[files.size()];
//        files.toArray(strings);
//        new HttpUtilsRequst<>(this, HttpUtilsRequst.retrofitClient.create(UserApi.class).authUser(
//                UserHelper.getToken(),bind.phone.getText().toString(),bind.wx.getText().toString(),strings
//        )).setmOnRequstSuccessListener((HttpUtilsRequst.OnRequstSuccessListener<String>) o -> {
//            startActivity(new Intent(EditeAuthUserActivity.this,AuthenticationUserActivity.class).putExtra("type",AuthenticationZhuboActivity.TYPE_PROGROSS));
//            finish();
//        }).start();
    }

    private ProgrossDialog progrossDialog;

    private void authUser2() {
        List<String> imgs = new ArrayList<>();
        for (int i = 0; i < bind.picSelectorView.getSelectorUrls().size(); i++) {
            if (bind.picSelectorView.getSelectorUrls().get(i).getItemType() == 0) {
                continue;
            }
            imgs.add(bind.picSelectorView.getSelectorUrls().get(i).getImg());
        }
        PutObjectSamplesApi.upLoadFils(imgs)
                .flatMap(strings -> HttpUtilsRequst.retrofitClient.create(UserApi.class).
                        authUser(
                                UserHelper.getToken(), bind.phone.getText().toString(), bind.wx.getText().toString(), new Gson().toJson(strings)))
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseVo<String>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        if (progrossDialog == null) {
                            progrossDialog = new ProgrossDialog(EditeAuthUserActivity.this);
                        }
                        progrossDialog.show();
                    }

                    @Override
                    public void onNext(BaseVo<String> tBaseVo) {
                        if (tBaseVo.getCode() == 1) {
                            startActivity(new Intent(EditeAuthUserActivity.this, AuthenticationUserActivity.class).
                                    putExtra("type", AuthenticationZhuboActivity.TYPE_PROGROSS));
                            finish();
                        } else {
                            toast(tBaseVo.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        progrossDialog.dismiss();
                    }

                    @Override
                    public void onComplete() {
                        progrossDialog.dismiss();
                    }
                });
    }

}
